package com.example.identifyvisio.service;


import com.example.identifyvisio.model.R;
import com.example.identifyvisio.model.component.CoordinatesAndParent;
import com.example.identifyvisio.model.component.Position;
import com.example.identifyvisio.model.component.Size;
import com.example.identifyvisio.model.node.Ports;
import com.example.identifyvisio.model.node.serviceNode.*;
import com.example.identifyvisio.model.node.startingnode.Body;

import java.util.*;

import static com.example.identifyvisio.utis.NodeValueUtil.getObtainCoordinates;
import static com.example.identifyvisio.utis.NodeValueUtil.getPorts;


/**
 * 服务节点
 */
public class ServiceNodeService {

    public static R<ServiceNode> getServiceNode(HashMap<Integer, Map<String, String>> dataMap, Integer shapeID,
                                               Map<String, String> shapeMap, HashMap<String, List<Position>> positionMap) {
        ServiceNode serviceNode = new ServiceNode();


        //获取坐标和父节点id
        CoordinatesAndParent obtainCoordinates = getObtainCoordinates(dataMap, shapeMap, positionMap);

        serviceNode.setPosition(obtainCoordinates.getPosition());
        serviceNode.setParent(obtainCoordinates.getParent());
        Size obtainSize = obtainCoordinates.getSize();
        Size size = new Size("流程");
        if (obtainSize.getWidth() != 0.0){
            size.setWidth(obtainSize.getWidth());
        }
        if (obtainSize.getHeight() != 0.0){
            size.setHeight(obtainSize.getHeight());
        }
        serviceNode.setSize(size);



        TextWrap textWrap = new TextWrap();
        textWrap.setText(shapeMap.get("text"));
        textWrap.setEllipsis(true);
        textWrap.setHeight("50%");
        textWrap.setWidth(-10);
        textWrap.setBreakWord(true);
        Text text = new Text(8,"#fff",textWrap);

        Body body = new Body("#3377fa","#3377fa",6,6);
        Attrs attrs1 = new Attrs(body,text);
        serviceNode.setAttrs(attrs1);
        serviceNode.setShape("custom-rect");

        String uniqueID = shapeMap.get("UniqueID");
        serviceNode.setId(uniqueID);

        ServiceData serviceData = new ServiceData();
        serviceData.setName(shapeMap.get("text"));
        serviceData.setType("node");
        serviceData.setLabel("ServiceTask1");

        serviceData.setStateId(dataMap.get(shapeID).get("stateId"));
        serviceData.setStateType("ServiceTask");

        StateProps stateProps = new StateProps();
        stateProps.setServiceName("SlpProxyInvoker");
        stateProps.setServiceMethod("invoke");
        stateProps.set_temp_service_type_chinese_name("产品服务");

        ArrayList<Input> inputArrayList = new ArrayList<>();
        Input input = new Input();
        input.set_conductor_service_name_key_("");
        input.set_conductor_service_method_key_("");
        input.set_conductor_service_version_key_("");
        input.set_conductor_service_type_key_("");
        input.set_conductor_level_key_("0");
        input.set_business_key_("$Sequence.BUSINESS_KEY|SIMPLE");
        inputArrayList.add(input);

        stateProps.setInput(inputArrayList);
        stateProps.setOutput(null);

        HashMap<String, String> statusMap = new HashMap<>();
        statusMap.put("$Exception{java.lang.Exception}","UN");
        statusMap.put("[sysHead][retCd] == '000000'","SU");
        statusMap.put("[sysHead][retCd] != '000000'","FA");
        stateProps.setStatus(statusMap);

        ArrayList<String> list = new ArrayList<>();
        stateProps.setRetry(list);

        serviceData.setStateProps(stateProps);

        serviceNode.setData(serviceData);

        Ports ports = getPorts();
        serviceNode.setPorts(ports);

        serviceNode.setZIndex("10");

        R<ServiceNode> serviceNodeR = new R<>();
        serviceNodeR.setData(serviceNode);

        return serviceNodeR;
    }
}
